clear all; close all; clc

% IMPORTANT: Please run the script 'RUN_ME_Analyze_data.m' first. 
% This code generates a table with all out-of-sample activation dates across counties.  

%% Choose variables and parameters
alphamin           = 2;       % choose minimum number of positive derivative events for the loop
strategy           = 'fast';  % choose strategy fast vs slow: fast picks up the minimum optimal alpha and threshold values (bottom-left of a rectangle), while slow picks up the maximum optimal values

%% Load data
load vec_state.mat;


% Load weeks
table_weeks = readtable('weeks_new_table.xlsx');
weeks       = char(table2array(table_weeks));
clear table_weeks

% Load Rt data and event weeks
table_events = readtable('data_events_omicron_shifted_state_level.xlsx');

% Vector with digital/predictor variable names
vec_var1{1}  = 'gt_covid';
vec_var1{2}  = 'gt_covid_19';
vec_var1{3}  = 'gt_howLongDoesCovidLast';
vec_var1{4}  = 'gt_covidSymptoms';
vec_var1{5}  = 'gt_covid_19Who';
vec_var1{6}  = 'up2date';
vec_var1{7}  = 'twitter_state';
vec_var1{8}  = 'gt_fever';
vec_var1{9}  = 'gt_chestPain';
vec_var1{10} = 'gt_afterCovidVaccine';
vec_var1{11} = 'gt_sideEffectsOfVaccine';
vec_var1{12} = 'gt_effectsOfCovidVaccine';
vec_var1{13} = 'JHU_cases';
vec_var1{14} = 'JHU_deaths';



for ind_var1 = 1:length(vec_var1) % start loop on predictor variables
    var1     = vec_var1{ind_var1}; % set predictor variable
    
    % start loop on counties 
    for ind_state =1:length(vec_state)
        
        % Find events for the state
        aux_ind_state              = contains(string(table_events{:,2}),vec_state(ind_state));
        table_selected_events       = table_events(aux_ind_state,:);
        
        for  ind_last_train_wave = 1:size(table_selected_events,1)-1 % open file training up to wave ind_last_train_wave and testing wave ind_last_train_wave+1
            
             filename = ['result_files_reproducible/Results_omicron_shifted_parag_prob_state_level_training_upto_wave_' num2str(ind_last_train_wave) '_test_wave_' num2str(ind_last_train_wave+1) '_alphamin_' num2str(alphamin) '_strategy_' strategy '_signal_' var1 '_ind_state_' num2str(ind_state) '.mat'];
            
            if exist(filename, 'file') == 2
                load(filename,'activation_weeks_var1_test')
                
                % Store activation weeks for predictor variable
                for i1=1:length(activation_weeks_var1_test)
                    aux_activation_weeks_var1_test(i1)= activation_weeks_var1_test{i1};
                end
                cell_unique_activation_weeks_var1_test{ind_state,ind_var1,ind_last_train_wave} = unique(aux_activation_weeks_var1_test);
                
                % Find last week of training set
                aux_ind_endweek_train              = datetime(weeks)==table_selected_events{ind_last_train_wave,5};
                endweek_train                      = find(aux_ind_endweek_train);
                
                % Find index of last week in test dataset
                aux_ind_endweek_test                  = datetime(weeks)==table_selected_events{ind_last_train_wave+1,5}; %
                endweek_test                          = find(aux_ind_endweek_test);
                
                % we count activations occuring only between the end of
                % last training wave and end of test wave
                testweek_fortables                    = intersect(cell_unique_activation_weeks_var1_test{ind_state,ind_var1,ind_last_train_wave},endweek_train+1:endweek_test);
                
                % if there are activations in that period, proceed
                if  ~isempty(testweek_fortables)
                    
                    cell_dates_for_tables{ind_state,ind_var1,ind_last_train_wave} = weeks(testweek_fortables,:); % store weeks in 3D cell array
                end
                
                % clear variables for loop in training waves
                clearvars -except ind_last_train_wave data_table alphamin strategy vec_state vec_var1 count var1 ...
                    ind_var1 ind_state cell_unique_activation_weeks_var1_test...
                    table_events table_selected_events weeks cell_dates_for_tables
            end
        end
        
        clear table_selected_events ind_last_train_wave
        
        
    end
    
    clear var1
end


% reset indicator for another loop
clear ind_state ind_var1 ind_last_train_wave 

% set counter for table rows
count= 0;


% start loop to build table for Leo
for ind_var1 = 1:length(vec_var1) % start loop on predictor 
    for ind_state = 1:size(cell_dates_for_tables,1) % start loop on counties
        count = count+1; % update counter 
        
        for ind_last_train_wave =1 % get activation for first wave, if any.
            
            aux_dates = cell_dates_for_tables{ind_state,ind_var1,ind_last_train_wave};
            
            if ~isempty(aux_dates)  % if statement to build list_dates only if there was some activation
                list_dates = aux_dates;
            else 
                list_dates = blanks(1); % if no activation, leave it blank
            end
        end

        for ind_last_train_wave =2:size(cell_dates_for_tables,3) % for the upcoming training waves 
            
            aux_dates = cell_dates_for_tables{ind_state,ind_var1,ind_last_train_wave};
            if ~isempty(aux_dates)
                list_dates = char({list_dates;aux_dates});
            end
        end
        
        data_table(count,:) = {vec_var1(ind_var1) ,vec_state(ind_state),list_dates};
        clear list_dates
    end
end


 table_name       = ['activations_table.csv'];
 result_table = table(data_table);
 writetable(result_table,table_name)



